package com.ruoyi.novel.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.novel.config.NumberToChineseConverter;
import com.ruoyi.novel.domain.Bookshelf;
import com.ruoyi.novel.domain.ChapterDTO;
import com.ruoyi.novel.domain.Subscribe;
import com.ruoyi.novel.mapper.BookshelfMapper;
import com.ruoyi.novel.mapper.ChapterMapper;
import com.ruoyi.novel.mapper.CollectionMapper;
import com.ruoyi.novel.mapper.HomePageMapper;
import com.ruoyi.novel.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.TableView;
import java.util.Date;

@Service
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private BookshelfMapper bookshelfMapper;
    @Override
    @Transactional
    public Object getContent(String novelName, String orderNum,String userId) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        // 先查询该章节是否需要被订阅
        int isSub = collectionMapper.selectIsChapterId(orderNum,novelId);

        if(isSub == 1){
            // 需要订阅，该用户是否订阅了该章节。
            Long chapterId = collectionMapper.selectChapterId(orderNum,novelId);
            Subscribe subscribe = collectionMapper.selectSubscribe(userId, chapterId);
            if(subscribe == null){
                return "你还没订阅该章节，请订阅后查看";
            }else{
                ChapterDTO content = chapterMapper.getContent(novelId, orderNum);
                return content;
            }
        }
        ChapterDTO content = chapterMapper.getContent(novelId, orderNum);
        updateChapter(Long.valueOf(userId), novelId, orderNum);

        return content;
    }
    @Override
    public ChapterDTO getContentForAuthor(String novelName, String orderNum) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        ChapterDTO content = chapterMapper.getContent(novelId, orderNum);
        return content;
    }
    @Override
    @Transactional
    public ChapterDTO getIsChapter(String novelName, String orderNum,Long userId) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        // 2. 将当前章节号（中文数字）转换为阿拉伯数字
        int currentOrder = NumberToChineseConverter.chineseToArabic(orderNum);
        // 3. 计算下一章节号（阿拉伯数字）
        int nextOrder = currentOrder + 1;
        // 4. 将下一章节号转换回中文数字
        String nextOrderNum = NumberToChineseConverter.arabicToChinese(nextOrder);
        String nextChapterName = "第"+nextOrderNum+"章";
        ChapterDTO content = chapterMapper.getContent(novelId, nextChapterName);
        if(content == null){
            return null;
        }
        updateChapter(Long.valueOf(userId), novelId, orderNum);
        return content;
    }

    @Override
    @Transactional
    public ChapterDTO getPreChapter(String novelName, String orderNum,Long userId) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        // 2. 将当前章节号（中文数字）转换为阿拉伯数字
        int currentOrder = NumberToChineseConverter.chineseToArabic(orderNum);
        if(currentOrder == 1){return null;}
        // 3. 计算下一章节号（阿拉伯数字）
        int nextOrder = currentOrder - 1;
        // 4. 将下一章节号转换回中文数字
        String nextOrderNum = NumberToChineseConverter.arabicToChinese(nextOrder);
        String nextChapterName = "第"+nextOrderNum+"章";
        ChapterDTO content = chapterMapper.getContent(novelId, nextChapterName);
        if(content == null){return null;}

        updateChapter(Long.valueOf(userId), novelId, orderNum);
        return content;
    }
    // 更新最近读的章节
    public int updateChapter(Long userId, Long novelId, String orderNum) {
        int isBelieve = bookshelfMapper.selectBookshelf(userId, novelId);
        if(isBelieve == 1) {
            Bookshelf bookshelf = new Bookshelf();
            bookshelf.setUserId(Long.valueOf(userId));
            bookshelf.setNovelId(novelId);
            bookshelf.setReadStatus(1L);
            bookshelf.setLastChapterNum(orderNum);
            bookshelf.setUpdateTime(new Date());
            int isSuc = bookshelfMapper.updateBookshelf(bookshelf);
            return isSuc;
        } else{
            return 0;
        }
    }

}
